//
//  PPNotificationSetTableViewController.m
//  pet
//
//  Created by 陈无忌 on 1/14/15.
//  Copyright (c) 2015 whalefin. All rights reserved.
//

#import "PPNotificationSetTableViewController.h"
#import "PPSystemConfiguraton.h"
#import "supportingHeadFiles.h"

@interface PPNotificationSetTableViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *voiceSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *shockSwitch;
@property (weak, nonatomic) IBOutlet UILabel *noteLabel;

@property (nonatomic) BOOL isVoice;
@property (nonatomic) BOOL isShock;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@property (weak, nonatomic) PPSystemConfiguraton *config;

@end

@implementation PPNotificationSetTableViewController
- (PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    NSString *url = [PPAPIList getAPIList].PPChangeUserSetting;
    NSDictionary *params = @{@"userId":@(self.config.userInfo.id),@"voice":@(self.voiceSwitch.on),@"shock":@(self.shockSwitch.on)};
    [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.config getUserSettingwithCompletion:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    if ([UIApplication instancesRespondToSelector:@selector(isRegisteredForRemoteNotifications)]) {
        if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
            self.statusLabel.text = @"已开启";
        }else{
            self.statusLabel.text = @"未开启";
        }
    } else {
        UIRemoteNotificationType status = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (status == UIRemoteNotificationTypeNone)
        {
            NSLog(@"User doesn't want to receive push-notifications");
            self.statusLabel.text = @"未开启";
        }else{
            self.statusLabel.text = @"已开启";
        }

    }
    self.noteLabel.lineBreakMode = NSLineBreakByCharWrapping;
    self.noteLabel.numberOfLines = 0;
    [self.noteLabel sizeToFit];
    
    self.voiceSwitch.on = self.config.userSetting.voice;
    self.shockSwitch.on = self.config.userSetting.shock;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
